﻿using System;
using System.Data;
using AjaxPro;
using WorkFlow.DBProxy;
using WorkFlow.Engine;
namespace WorkFlow.Desinger
{
    [AjaxNamespace("ns")]
    public partial class ProcessManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(GetType(), Page);
            Session["USERID"] = 1;
        }
        [AjaxMethod]
         public string[] GetProcessInfo(string ver)
         {
             int version;
            if (!int.TryParse(ver, out version)) return null ;
            string sql = string.Format(@"select f_id as PID,f_name as PNAME,
                                                        f_version as PVER,f_modify_person as PPer,
                                                        f_start_date as STARTTIME,f_end_date as ENDDATE,f_is_activated as ISACT
                                                         from tr_processes where f_version={0}",version);
            DataTable dt = DB.MapToDataTable(sql);
            if (dt == null || dt.Rows.Count == 0) return null;
            string[] list = new string[7];
            list[0] = SecHtmlID.EncriptID(dt.Rows[0]["PID"].ToString());
            list[1] = dt.Rows[0]["PNAME"] == DBNull.Value ? "" : dt.Rows[0]["PNAME"].ToString();
            list[2] = dt.Rows[0]["PVER"].ToString();
            list[3] = dt.Rows[0]["PPer"].ToString();
            list[4] = DateTime.Parse(dt.Rows[0]["STARTTIME"].ToString()).ToShortDateString().Replace('/', '-');
            list[5] = DateTime.Parse(dt.Rows[0]["ENDDATE"].ToString()).ToShortDateString().Replace('/', '-');
            if (Convert.ToInt32(dt.Rows[0]["ISACT"]) == WfFieldCode.DbFalse)
            {
                list[6] = "N";
            }
            else
            {
                list[6] = "Y";
            }
            return list;
         }

         [AjaxMethod]
         public DataTable GetVersions()
         {
             const string sql = @"select F_VERSION  as VER FROM TR_PROCESSES ORDER BY F_VERSION desc";
             return DB.MapToDataTable(sql);
         }

        [AjaxMethod]
        public string SaveChanged(string[] strs)
        {
            //id name startdate enddate isacted
            strs[0] = SecHtmlID.DecodeID(strs[0]);
            strs[1]= strs[1].Replace('-', ' ').Replace(';', ' ').Replace('/', ' ');
            DateTime dttemp;
            if (!DateTime.TryParse(strs[2], out dttemp) || !DateTime.TryParse(strs[3], out dttemp)) return "日期格式错误!";
            int isActed = strs[4].Equals("Y") ? WfFieldCode.DbTrue : WfFieldCode.DbFalse;
            string sql =
                string.Format(
                    @"update tr_processes set f_name='{0}' ,f_modify_person={1} ,f_start_date=to_date('{2}','yyyy-mm-dd HH24:MI:SS'),f_end_date=to_date('{3}','yyyy-mm-dd HH24:MI:SS'),f_is_activated={4},f_modify_date=sysdate where f_id={5}",strs[1],Session["USERID"],strs[2],strs[3],isActed,strs[0]);
            if (DB.ExecuteSql(sql).Equals(DB.OK)) return "保存成功 !";
            return "保存失败!";
        }
    }
}